﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceMan.Entities.Attachments
{
    class C_EFF_LightFlash : I_Effect
    {
        List<structure> LightCones;
        C_Drawing Core;
        Random random;
        float rotation1, rotation2, rotation3, size, step, baseSize, stepSize, lifeTime, rotateSpeed1, rotateSpeed2, rotateSpeed3;
        bool stopatmiddle, stopped, continueStep, playing;

        struct structure
        {
            public C_Drawing draw;
            public Rectangle size;
            public float rotation;
            public int layer;
        }

        public C_EFF_LightFlash(Vector2 pos, float size, float rotateSpeed, float lifeTime ,bool stopatmiddle)
        {
            random = new Random();
            this.lifeTime = lifeTime;
            this.stopatmiddle = stopatmiddle;
            this.baseSize = size;
            base.Position = Position;
            this.rotateSpeed1 = -rotateSpeed + (float)(rotateSpeed * 2 * random.NextDouble());
            this.rotateSpeed2 = -rotateSpeed + (float)(rotateSpeed * 2 * random.NextDouble());
            this.rotateSpeed3 = -rotateSpeed + (float)(rotateSpeed * 2 * random.NextDouble());

            LightCones = new List<structure>();
            
            int loops = random.Next(4, 7);
            for (int i = 0; i < loops; i++)
            {
                C_Drawing _temp = S_Global.getDraw("GFX/conelight_512x1024");
                structure _tempstruct;
                _tempstruct.draw = _temp;
                _tempstruct.rotation = (float)random.NextDouble() * (float)Math.PI * 2;
                _tempstruct.size = new Rectangle(0, 0, (int)(50 + (random.NextDouble() * 206) * size), (int)((512 + (int)(random.NextDouble() * 512)) * size));
                _tempstruct.layer = random.Next(0, 2);
                _temp.Update(Vector2.Zero, (float)((Math.PI * 2) * random.NextDouble()), _tempstruct.size.Width, null, false, null);

                LightCones.Add(_tempstruct);
            }

            stepSize = 1 / lifeTime;
            Core = S_Global.getDraw("GFX/Glow_Spot_512x512");
            
        }

        public override void UserInput1()
        {
            continueStep = false;
            step = (float)(3*Math.PI / 2);
            playing = true;
            foreach (structure _struct in LightCones)
            {
                _struct.draw.Update(base.Position, (float)(random.NextDouble() * (Math.PI * 2)), null, null, false, null);
            }
        }

        public override void UserInput2()
        {
            continueStep = true;
        }

        protected override void EffectUpdate()
        {
            Color color = new Color(255, 255, 255, 0);
            if (playing)
            {
                step += stepSize * S_Global.getTimescale();
                if (stopatmiddle && !continueStep && step >= (float)Math.PI * 2.5f)
                {
                    step = (float)Math.PI * 2.5f;
                }

                rotation1 += rotateSpeed1 * S_Global.getTimescale();
                rotation2 += rotateSpeed2 * S_Global.getTimescale();
                rotation3 += rotateSpeed3 * S_Global.getTimescale();
                if (step > Math.PI * 3.5)
                {
                    playing = false;
                }
                step = (MathHelper.Clamp(step, (float)(3 * Math.PI / 2), (float)(Math.PI) * 3.5f));
                size = step;
                size = ((float)Math.Sin(size) + 1) * 0.5f;

                color = new Color((int)255, (int)255, (int)255, (int)(255 * size));

                
            }
            Core.Update(Position, 0f, (int)(1024 * size * baseSize), (int)(1024 * size * baseSize), true, color);
                foreach (structure _struct in LightCones)
                {
                    Rectangle _rect = _struct.size;
                    Vector2 _aspectRatio = new Vector2(_rect.Width, _rect.Height);
                    _aspectRatio.Normalize();
                    int _size = (int)(1024 * size);
                    float _rotation;
                    if (_struct.layer == 0)
                    {
                        _rotation = rotation1;
                    }

                    else if (_struct.layer == 0)
                    {
                        _rotation = rotation2;
                    }

                    else
                    {
                        _rotation = rotation3;
                    }

                    _struct.draw.Update(base.Position, _struct.rotation + _rotation, (int)(_aspectRatio.X * _size * baseSize), (int)(_aspectRatio.Y * _size * baseSize), false, color);
                
            }
        }


        protected override void EffectDraw(SpriteBatch spriteBatch, DrawMode drawMode, Rectangle drawSpace)
        {
            Core.Draw(spriteBatch, drawMode, drawSpace);
            foreach (structure _struct in LightCones)
            {
                _struct.draw.Draw(spriteBatch, drawMode, drawSpace);
            }
        }
    }
}
